import unittest, requests


class Wdds(unittest.TestCase):

    def setUp(self) -> None:
        print('开始测试')

    def tearDown(self) -> None:
        print('测试结束')

    token = None
    host_dz = '10.59.9.18'

    # def test_wdds_01_register(self):
    #     wdds_register = requests.post(url='http://'+Wdds.host_dz+'/small/user/v1/register', json={"phone": "15985645484", "pwd": "123456Aa"})
    #     print(wdds_register.json())
    # 登录
    userIdn = None
    sessionId = None
    def test_wdds_02_login(self):
        wdds_login = requests.post(url='http://' + Wdds.host_dz + '/small/user/v1/login',
                                   json={"phone": "15985645484", "pwd": "123456Aa"})
        print(wdds_login.json())
        Wdds.userIdn = wdds_login.json()['result']['userId']
        Wdds.sessionId = wdds_login.json()['result']['sessionId']
    # 添加地址
    # def test_wdds_03_address(self):
    #     wdds_address = requests.post(url='http://' + Wdds.host_dz + '/small/user/verify/v1/addReceiveAddress',
    #                                  params={"realName": "小高先生", "phone": "15985645484",
    #                                          "address": "北京 海淀区 八维", "zipCode": "101010"},
    #                                  headers={"userId": str(Wdds.userIdn), "sessionId": str(Wdds.sessionId)})
    #     print(wdds_address.json())

    # 获取地址信息
    get_address = None
    def test_wdds_04_get_address(self):
        wdds_get_addres = requests.get(url='http://'+Wdds.host_dz+'/small/user/verify/v1/receiveAddressList',
                                       headers={"userId": str(Wdds.userIdn), "sessionId": str(Wdds.sessionId)})
        print(wdds_get_addres.json())
        Wdds.get_address = wdds_get_addres.json()['result'][0]['id']
        print(Wdds.get_address)

    # 首页商品列表
    spid1 = None
    spid2 = None
    def test_wdds_05_list(self):
        wdds_list = requests.get(url='http://'+Wdds.host_dz+'/small/commodity/v1/commodityList')
        print(wdds_list.json())
        Wdds.spid1 = wdds_list.json()['result']['rxxp']['commodityList'][0]['commodityId']
        Wdds.spid2 = wdds_list.json()['result']['rxxp']['commodityList'][1]['commodityId']
        print(Wdds.spid1)
        print(Wdds.spid2)


    # 商品1详情
    price1 = None
    def test_wdds_06_xq1(self):
        wdds_xq1 = requests.get(url='http://'+Wdds.host_dz+'/small/commodity/v1/findCommodityDetailsById',
                             headers={"userId": str(Wdds.userIdn), "sessionId": str(Wdds.sessionId)},
                             params={'commodityId':str(Wdds.spid1)})
        print(wdds_xq1.json())
        Wdds.price1 = wdds_xq1.json()['result']['price']
        print(Wdds.price1)

    # 商品2详情
    price2 = None
    def test_wdds_07_xq2(self):
        wdds_xq2 = requests.get(url='http://' + Wdds.host_dz + '/small/commodity/v1/findCommodityDetailsById',
                               headers={"userId": str(Wdds.userIdn), "sessionId": str(Wdds.sessionId)},
                               params={'commodityId': str(Wdds.spid2)})
        print(wdds_xq2.json())
        Wdds.price2 = wdds_xq2.json()['result']['price']
        print(Wdds.price2)


    # 创建订单
    def test_wdds_08_create(self):
        wdds_create = requests.post(url='http://'+Wdds.host_dz+'/small/order/verify/v1/createOrder',
                                    headers={"userId": str(Wdds.userIdn), "sessionId": str(Wdds.sessionId)},
                                    params={"orderInfo": [{'commodityId':str(Wdds.spid1),'amount':2},{'commodityId':str(Wdds.spid2),'amount':1}], "totalPrice": 2*float(Wdds.price1)+float(Wdds.price2),
                                            "addressId": str(Wdds.get_address)})
        print(wdds_create.json())

if __name__ == '__main__':
    unittest.main()
